package com.ostrich.module_home.discovery

import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.LinearLayoutManager
import com.alibaba.android.arouter.launcher.ARouter
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.listener.OnItemChildClickListener
import com.ostrich.library_base.BaseMvvmFragment
import com.ostrich.library_common.bean.VideoBean
import com.ostrich.library_common.route.RouterActivityPath
import com.ostrich.library_common.utils.LoggerUtils
import com.ostrich.module_home.R
import com.ostrich.module_home.databinding.HomeFragmentDiscoveryBinding
import com.ostrich.module_home.discovery.adapter.DiscoveryAdapter

class DiscoveryFragment : BaseMvvmFragment<HomeFragmentDiscoveryBinding,DiscoveryViewModel>() {
    private lateinit var discoveryAdapter: DiscoveryAdapter
    override fun initView() {
        binding?.let {
            discoveryAdapter = DiscoveryAdapter()
            it.rv.adapter = discoveryAdapter
            it.rv.layoutManager = LinearLayoutManager(context)
            it.rv.setHasFixedSize(true)
        }

    }

    override fun initData() {
        viewModel.discovery().observe(this){
            LoggerUtils.d(TAG,it.toString())
            discoveryAdapter.setList(it.itemList)
        }

    }

//    var videoTitle: String,
//    var category: String,
//    var video_description: String,
//    var collectionCount:Int,
//    var shareCount:Int,
//    var avatar: String,
//    var nickName: String,
//    var userDescription: String,
//    var playerUrl: String,
//    var blurredUrl: String,
//    var videoId:Int
    override fun initListener() {
        //设置item子控件的点击事件
        discoveryAdapter.addChildClickViewIds(R.id.iv_video_cover)
        discoveryAdapter.setOnItemChildClickListener { adapter, view, position ->
            val discoveryEntity = discoveryAdapter.data[position]
            val videoBean = VideoBean(
                discoveryEntity.data.title,
                discoveryEntity.data.category,
                discoveryEntity.data.description,
                discoveryEntity.data.consumption.collectionCount,
                discoveryEntity.data.consumption.shareCount,
                discoveryEntity.data.author.icon,
                discoveryEntity.data.author.name,
                discoveryEntity.data.author.description,
                discoveryEntity.data.playUrl,
                discoveryEntity.data.cover.blurred,
                discoveryEntity.data.id

            )

            ARouter.getInstance()
                .build(RouterActivityPath.PAGER_PLAYER)
                .withParcelable("video",videoBean )
                .navigation()
        }

    }

    override fun bindLayout(): Int {
        return R.layout.home_fragment_discovery
    }

    override fun getJVMClass(): Class<DiscoveryViewModel> {
        return DiscoveryViewModel::class.java
    }

    companion object{
        const val TAG = "DiscoveryFragment"
    }

}